package com.haois.mp.database.module.offaccounts.controller;


import com.google.gson.Gson;
import com.haois.mp.database.exception.OutException;
import me.chanjar.weixin.common.exception.WxErrorException;
import com.haois.mp.database.module.basic.controller.BaseController;
import com.haois.mp.database.commom.CommonConstants;
import com.haois.mp.database.module.remote.service.IGroupService;
import com.haois.mp.database.util.mp.DataCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/wxmp/group")
public class GroupController extends BaseController {

    private static final Logger logger = LoggerFactory
            .getLogger(GroupController.class);

    @Autowired
    private IGroupService iGroupService;

    private Gson gson = new Gson();

    @RequestMapping(value = "/toGroup")
    public ModelAndView toBindCardList(HttpServletRequest request, String fromPage) throws WxErrorException, OutException {
        String access_token = (String) request.getAttribute("access_token");
        Map result = iGroupService.getGroupList(CommonConstants.GROUP_01, access_token);
        DataCheck.codeNot0X(result);
        ModelAndView mv = new ModelAndView("/group/groupList");
        mv.addObject("result", gson.toJson(result));
        mv.addObject("fromPage", "inGroup");
        return mv;
    }

    @RequestMapping(value = "/group")
    public ModelAndView toGroup(HttpServletRequest request) throws WxErrorException, OutException {
        ModelAndView mv = new ModelAndView("/group/group");
        String access_token = (String) request.getAttribute("access_token");
        Map result = iGroupService.getGroupList(CommonConstants.GROUP_01, access_token);
        String fromPage = request.getParameter("fromPage");
        fromPage = fromPage == null ? "inGroup" : fromPage;
        DataCheck.codeNot0X(result);
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        mv.addObject("result", gson.toJson(result));
        mv.addObject("fromPage", fromPage);
        return mv;
    }

    @RequestMapping(value = "/toMobile")
    public ModelAndView toMobile(String oid, HttpServletRequest request)
            throws WxErrorException, OutException {
        ModelAndView mv = new ModelAndView("/group/mobile");
        String fromPage = request.getParameter("fromPage");
        mv.addObject("fromPage", fromPage);
        mv.addObject("oid", oid);
//        mv.addObject("result", gson.toJson(result));
        return mv;
    }

    @RequestMapping(value = "/info")
    public ModelAndView info(HttpServletRequest request) throws WxErrorException, OutException {
        ModelAndView mv = new ModelAndView("/group/infoList");
        String access_token = (String) request.getAttribute("access_token");
        Map result = iGroupService.getGroupList(CommonConstants.GROUP_01, access_token);
        DataCheck.codeNot0X(result);
        mv.addObject("result", gson.toJson(result));
        mv.addObject("fromPage", "");
        return mv;
    }

    @RequestMapping(value = "/notice")
    public ModelAndView notice(HttpServletRequest request) throws WxErrorException {
        ModelAndView mv = new ModelAndView("/group/noticeList");
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        mv.addObject("fromPage", "notice");
        return mv;
    }

    @RequestMapping(value = "/frame")
    public ModelAndView frame(HttpServletRequest request) throws WxErrorException, OutException {
        ModelAndView mv = new ModelAndView("/group/frameList");
        String access_token = (String) request.getAttribute("access_token");
        Map result = iGroupService.getGroupList(CommonConstants.GROUP_01, access_token);
        DataCheck.codeNot0X(result);
        mv.addObject("result", gson.toJson(result));
        mv.addObject("fromPage", "");
        return mv;
    }

    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest request) throws WxErrorException {
        ModelAndView mv = new ModelAndView("/group/groupAdd");
        mv.addObject("fromUser", DataCheck.getOpenId(request));
        mv.addObject("fromPage", "notice");
        return mv;
    }

}
